#!/bin/bash

# decompress an image, and remove the .zraw if it was an mhd.
# 

if [ $# = 0 ]
then
  echo "pxdecompressimage"
  echo
  echo "Usage: pxdecompressimage <inputfilename1> ..."
  echo
  echo "If the input is a compressed mhd, the zraw file is deleted."
  echo
  exit 64
fi

# the list of input images
inputimages="$*"

for inputimage in $inputimages
do

  # decompress
  pxcastconvert -in "$inputimage" -out "$inputimage"
  if [ "$?" != "0" ]
  then
    exit 65
  fi

  # check for .mhd
  ext=`expr match "$inputimage" '.*\([.][mM][hH][dD]\)'`
  ismhd="$?"

  # if so, remove the .zraw file, if it exists
  if [ "$ismhd" = "0" ]
  then
    inputimageraw="${inputimage/%$ext/.zraw}"

    if [ -e "$inputimageraw" ]
    then
      rm "$inputimageraw"
    fi
  fi
  
done

# return a value
exit 0

